package com.sy.controller;

import com.sy.entity.StudyFile;
import com.sy.exception.WebException;
import com.sy.mapper.StudyFileMapper;
import com.sy.utils.CommonUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.Part;
import java.io.*;
import java.net.URLDecoder;

import static com.sy.constant.AppConstant.*;


/**
 * 上传下载控制层
 */
@RestController
@MultipartConfig
//Tomcat中必须设置@MultipartConfig标注才能使用getPart()相关API，否则会返回null
//SpringMVC中使用这个注解需要在web.xml文件中配置<multipart-config>标签
public class StudyFileController extends BaseController {


    //上传单个文件
    @RequestMapping(value = "file",method = RequestMethod.POST,params = "type=upload")
    public StudyFile uploadOne() throws IOException, ServletException {
        Integer id = getSessionUserId();
        Part part = req.getPart("file");
        StudyFile studyFile = studyFileService.uploadOneFile(id,part);
        return studyFile;
    }

    //下载文件
    @RequestMapping(value = "file/{id}",method = RequestMethod.POST,params = {"type=download","fileName","fileEx"})
    public boolean download(@PathVariable("id")String id,String fileName,String fileEx) throws IOException {
        //动态匹配Content-Type
        String contentType = CommonUtils.getContentType(fileEx);
        if(StringUtils.isBlank(contentType)){
            throw new WebException("未能匹配该类型文件！");
        }
        String name = URLDecoder.decode(fileName, "UTF-8");
        System.out.println(fileName);
        System.out.println(name);
        System.out.println(contentType);
        //告诉浏览器输出内容
        res.setContentType(contentType);
        ServletOutputStream os = res.getOutputStream();
        // 设置响应数据需保存为文件，同时指定下载信息
        res.addHeader("Content-Disposition","attachment;filename="+ name);
        studyFileService.getFile(id, os);
        return true;
    }

    //预览文件
    @RequestMapping(value = "files/{id}",method = RequestMethod.GET,params = {"type=preview","fileName","fileEx"})
    public boolean preview(@PathVariable("id") String id,String fileName,String fileEx) throws IOException {
        //动态匹配Content-Type
        String contentType = CommonUtils.getContentType(fileEx);
        if(StringUtils.isBlank(contentType)){
            throw new WebException("未能匹配该类型文件！");
        }

        String name = URLDecoder.decode(fileName, "UTF-8");
        //告诉浏览器输出内容
        res.setContentType(contentType);
        ServletOutputStream os= res.getOutputStream();
        // 设置响应数据需保存为文件，同时指定下载信息
        if(StringUtils.equals(fileEx,FILE_EXTENSION_JPG) || StringUtils.equals(fileEx,FILE_EXTENSION_JPE) || StringUtils.equals(fileEx,FILE_EXTENSION_JPEG)){
            res.setHeader("Content-Disposition", "jpeg");
        }else{
            res.setHeader("Content-Disposition", "attachment;filename="+name);
        }
        studyFileService.getFile(id, os);
        return true;
    }

    @RequestMapping(value="teacher",method=RequestMethod.DELETE,params = {"type=deleteStudyFile","id"})
    public boolean deleteStudyFile(String id){
        return studyFileService.deleteStudyFile(id);
    }
}
